$(function () {
    getUserInfo()
})

function getUserInfo() {
    $.ajax({
        type: 'GET',
        url: '/my/userinfo',
        // 请求头配置对象
        // headers: {
        //     Authorization: localStorage.getItem('token') || ''
        // },
        success: function (res) {
            console.log(res);
            if (res.status !== 0) return layui.layer.msg('获取失败!')
            renderAvatar(res.data)
        }
    })
}


// 渲染用户头像
function renderAvatar(user) {
    var name = user.nickname || user.username
    $('#welcome').html('欢迎&nbsp;' + name)
    if (user.user_pic !== null) {
        $('.layui-nav-img').attr('src', user.user_pic).show()
        $('.text-avatar').hide()
    } else {
        $('.layui-nav-img').hide()
        // 拿到名字第一个字符，.toUpperCase（）转换为大写
        $('.text-avatar').html(name[0].toUpperCase()).show()
    }
}


$('#btnLogout').on('click', function () {

    layer.confirm('确定退出?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        localStorage.removeItem('token');
        location.href = '../../login.html',
        layer.close(index)
    });

})